<template>
  <div>
    <!--添加-->
    <div class="addjl">
      <el-input size="small" v-model="jl.name" placeholder="添加职称..." prefix-icon="el-icon-plus"
                class="SysBasicAddInput">
      </el-input>
      <el-select style="margin: 0 10px 0 0px" size="small" v-model="jl.titleLevel" placeholder="职称等级">
        <el-option v-for="item in titleLevels" :key="item" :label="item" :value="item">
        </el-option>
      </el-select>
      <el-button size="small" icon="el-icon-plus" type="primary" @click="addJl">添加</el-button>
    </div>
    <div style="margin-top: 10px">
      <el-table :data="jls" stripe border size="small" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column prop="id" label="编号" width="55">
        </el-table-column>
        <el-table-column prop="name" label="职称名称" width="150">
        </el-table-column>
        <el-table-column prop="titleLevel" label="职称等级" width="150">
        </el-table-column>
        <el-table-column prop="createDate" label="创建时间" width="150">
        </el-table-column>
        <el-table-column prop="enable" label="是否启用" width="150">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.enabled" type="success">已启用</el-tag>
            <el-tag v-else type="danger">未启用</el-tag>
          </template>
        </el-table-column>
        <el-table-column
            label="操作">
          <template slot-scope="scope">
            <el-button
                size="mini"
                @click="showEditView(scope.row)">编辑
            </el-button>
            <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
        title="编辑职称"
        :visible.sync="dialogVisible"
        width="30%">
      <table>
        <tr>
          <td>
            <el-tag>职称名称</el-tag>
          </td>
          <td>
            <el-input size="small" style="margin-left: 8px" v-model="updateJl.name"></el-input>
          </td>
        </tr>
        <tr>
          <td>
            <el-tag>职称等级</el-tag>
          </td>
          <td>
            <el-select size="small" style="margin-left: 8px;margin-right: 8px"
                       v-model="updateJl.titleLevel" placeholder="职称等级">
              <el-option
                  v-for="item in titleLevels"
                  :key="item"
                  :label="item"
                  :value="item">
              </el-option>
            </el-select>
          </td>
        </tr>
        <tr>
          <td>
            <el-tag>是否启用</el-tag>
          </td>
          <td>
            <el-switch
                style="margin-left: 8px"
                v-model="updateJl.enabled"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-text="启用"
                inactive-text="禁用">
            </el-switch>
          </td>
        </tr>
      </table>
      <span slot="footer" class="dialog-footer">
                <el-button size="small" @click="dialogVisible = false">取 消</el-button>
                <el-button size="small" type="primary" @click="doUpdate">确 定</el-button>
            </span>
    </el-dialog>
    <el-button size="small" style="margin-top: 8px" type="danger" :disabled="this.multipleSelection.length==0"
               @click="deleteMany">批量删除
    </el-button>
  </div>
</template>

<script>
export default {
  name: "JoblebeMana",
  mounted() {
    this.initJl()
  },
  data() {
    return {
      multipleSelection: [],
      dialogVisible: false,
      updateJl: {
        name: '',
        titleLevel: '',
        enabled: '',
        createDate: ""
      },
      jls: [],
      titleLevels: [
        '正高级',
        '副高级',
        '中级',
        '初级',
        '员级'
      ],
      jl: {
        name: '',
        titleLevel: ""
      }
    }
  },
  methods: {
    doUpdate(row) {
      this.putRequest(this.api.jobLevel, this.updateJl).then(res => {
        if (res) {
          this.initJl()
          this.dialogVisible = false
        }
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    deleteMany() {
      this.$confirm('此操作将永久删除[' + this.multipleSelection.length + ']条职称, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let request = this.api.jobLevel + "?"
        this.multipleSelection.forEach(value => {
          request += "ids=" + value.id + "&"
        })
        this.deleteRequest(request).then(res => {
          if (res) {
            this.initJl()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleDelete(row) {
      const h = this.$createElement;
      this.$msgbox({
        title: '提示',
        message: h('p', null, [
          h('span', null, "此操作将永久删除\t"),
          h('i', {style: 'color: teal'}, row.name),
          h('span', null, "\t职称"),
        ]),
        showClose: false,
        showCancelButton: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        beforeClose: (action, instance, done) => {
          if (action === 'confirm') {
            instance.confirmButtonLoading = true;
            instance.confirmButtonText = '执行中...';
            setTimeout(() => {
              done();
              setTimeout(() => {
                instance.confirmButtonLoading = false;
              }, 300);
            }, 500);
          } else {
            done();
          }
        }
      }).then(action => {
        this.deleteRequest(this.api.jobLevel + "?ids=" + row.id).then(res => {
          if (res) {
            this.initJl()
          }
        })
      }).catch(cancel => {
        this.$message({type: 'info', message: "已取消删除"});
      })
    },
    showEditView(row) {
      this.dialogVisible = true
      Object.assign(this.updateJl, row)
      this.updateJl.createDate = ""
    },
    addJl() {
      if (this.jl.name && this.jl.titleLevel) {
        this.postRequest(this.api.jobLevel, this.jl).then(res => {
          if (res) {
            this.initJl()
          }
        })
      } else {
        this.$message.error("不能为空……")
      }
    },
    initJl() {
      this.getRequest(this.api.jobLevel).then(res => {
        if (res) {
          this.jls = res.data
        }
      })
    }
  }
}
</script>

<style scoped>
</style>